package com.itheima.admin.controller;


import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/channel")
@Api(tags = "频道管理")
public class AdChannelController {

    @Autowired
    private AdChannelService adChannelService;

    /**
     * 频道分页查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("频道分页查询")
    public PageResultVo pageList(@RequestBody ChannelPageRequestDto dto) {
        return adChannelService.list(dto);
    }


    /**
     * 新增频道
     *
     * @param adChannel
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增频道")
    public ResultVo add(@RequestBody AdChannel adChannel) {

        adChannel.setAsDefault(false);
        adChannel.setCreatedTime(new Date());

        if (adChannelService.save(adChannel)) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过id查询频道")
    public ResultVo findById(@PathVariable Integer id) {
        AdChannel adChannelById = adChannelService.getById(id);
        return ResultVo.ok(adChannelById);
    }

    @PostMapping("/update")
    @ApiOperation("更新频道")
    public ResultVo update(@RequestBody AdChannel adChannel) {
        if (adChannelService.updateById(adChannel)) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation("删除频道")
    public ResultVo dalete(@PathVariable Integer id){
        if (adChannelService.removeById(id)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

}
